<template>
  <a-modal
    :zIndex="zIndex"
    :title="title"
    :visible="visible"
    @cancel="handleCancel"
    width="650px"
    :cancelText="$t('关闭')"
    :confirmLoading="confirmLoading"
    v-drag
  >
    <template slot="footer">
      <a-button @click="handleCancel" :disabled="confirmLoading">{{ $t('取消') }}</a-button>
      <a-button type="primary" @click="handleOk" :disabled="confirmLoading">{{ $t('保存') }}</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="dataForm" :rules="rules" :model="dataForm">
        <a-form-model-item
          style="margin-bottom: 10px"
          :label="$t('名称')"
          :labelCol="{ span: 3 }"
          :wrapperCol="{ span: 18 }"
          prop="name"
        >
          <a-input v-model="dataForm.name" :placeholder="$t('请输入')" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
export default {
  name: 'AddUnitModal',
  components: {},
  data() {
    return {
      zIndex: 1002,
      form: this.$form.createForm(this),
      dataForm: {},
      title: this.$t('新增单位'),
      confirmLoading: false,
      visible: false,
      rules: {
        name: [{ required: true, message: this.$t('请输入名称') }],
      },
    }
  },
  methods: {
    show() {
      this.visible = true
      this.confirmLoading=false
      this.dataForm={}
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      let that = this
      let querydata = {}
      that.$refs.dataForm.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          querydata.name = that.dataForm.name
          httpAction('/goods/bisGoodsUnit/add', querydata, 'post').then((res) => {
            if(res.success){
              that.$emit('ok',res.result)
              that.close()
            }else{
              that.$message.error(res.message)
            }
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>